import math

import redis


class MyRedis:
    def __init__(self) -> None:
        self.pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password="")
        self.r = redis.Redis(connection_pool=self.pool)

# ------------------------------------------------- str -------------------------------------------------
    # str 存
    def set_str(self, key, v, time=0):
        if time > 0:
            self.r.setex(key, time, v)
        else:
            self.r.set(key, v)

    # str 取
    def get_str(self, key):
        value = self.r.get(key)
        if value:
            value = self.r.get(key).decode()
        return value

    def remove_str(self, key):
        self.r.delete(key)

    # 分布式锁setnx
    def setnx_str(self, key, value):
        return self.r.setnx(key, value)

    # 模糊查询
    def findall_str(self, key):
        return self.r.keys(key + '*')

    # 匹配删除
    def del_str(self, key):
        self.r.delete(*self.r.keys(key + '*'))


# ------------------------------------------------- hash -------------------------------------------------
    #  hash
    # 添加
    def hash_add(self, pkey, key, value):
        self.r.hset(pkey, key, value)

    # 获取属性值
    def hash_items_value(self, pkey, key):
        return self.r.hset(pkey, key)

    # 获取对象对应的所有属性
    def hash_getall(self, pkey):
        return self.r.hgetall(pkey)

    # # 自增自减 count为正时加负数时减
    # def hash_hincrby(self, pkey, key, count):
    #     self.r.hincrby(pkey, key, count)

    # # 自增自减 count为正数的时候加负数的时候减***
    # def hash_hincrby(self, pkey, key, count, type):
    #     if int(type) == 2:
    #         # 取绝对值math.fabs
    #         tc = int(math.fabs(int(count)))
    #         count = "-" + str(count)
    #         # 判断count<redis存
    #         number = self.hash_item_vhget_stralue(pkey, key)
    #         if tc >= int(number):
    #             return 'false'
    #     self.r.hincrby(pkey, key, count)

    # 判断某个key是否存在
    def is_exits(self, key):
        return self.r.exists(key)

    # 删除
    def del_hash(self, key):
        return self.r.delete(key)

    # 删除单个
    def del_one(self, pkey, key):
        return self.r.hdel(pkey, key)


# ------------------------------------------------- zset -------------------------------------------------
    # zset
    # 添加
    def zset_add(self, key, score, value):
        mapping = {score: value}
        self.r.zadd(key, mapping)

    # 查询所有
    def zset_getall(self, key):
        return self.r.zrange(key, 0, -1)

    # 删除
    def zset_del(self, key, value):
        return self.r.srem(key, value)

    # 区间查询
    def zset_byscore(self, key, min, max):
        return self.r.zrangebyscore(key, min, max)


# ------------------------------------------------- list -------------------------------------------------
    # list 添加
    def set_list(self, key, v, time=0):
        if time > 0:
            self.r.lpush(key, v)
        else:
            self.r.rpush(key, v)

    # list 获取
    def get_list(self, key):
        return self.r.lrange(key, 0, -1)

    def rpop_list(self, key):
        return self.r.rpop(key)


r = MyRedis()


# import redis, json
#
#
# class MyRedis():
#     def __init__(self):
#         pool = redis.ConnectionPool()
#         self.r = redis.Redis(connection_pool=pool)
#
#     def set_str(self, key, value, time=0):
#         if not time:
#             self.r.set(key, value)
#         else:
#             # 单位 秒
#             self.r.setex(key, time, value)
#
#     def get_str(self, key):
#         res = self.r.get(key)
#         if not res:
#             return res
#         else:
#             return res.decode()
#
#     def del_str(self, key):
#         self.r.delete(key)
#
#     # hash系列
#     def hset_str(self, name, key, value):
#         self.r.hset(name, key, value)
#
#     def hgetall_str(self, name):
#         res = self.r.hgetall(name)
#         if res:
#             data = {}
#             for i, j in res.items():
#                 data[i.decode()] = j.decode()
#             return data
#         else:
#             return False
#
#     def hget_str(self, name, key):
#         res = self.r.hget(name, key)
#         if res:
#             return res.decode()
#         else:
#             return False
#
#     def hdel_str(self, name, key):
#         self.r.hdel(name, key)
#
#     # set系列
#     def sadd_str(self, key, value):
#         self.r.sadd(key, value)
#
#     def smembers_str(self, key):
#         res = self.r.smembers(key)
